import store from '@/store'
// 登录接口
import request from '@/utils/request'
import axios from 'axios'

const baseURL = 'http://101.35.25.8:8000/v1_0/'
// import { Store } from 'vuex'
export function login (mobile, code) {
  return request({
    url: 'authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })
}

// 验证码接口
export function sendSMS (mobile) {
  return request({
    url: 'sms/codes/' + mobile
  })
}

// 获取用户的频道
export async function getUserChannels () {
  // 1.判断登录？
  // console.log(!store.state.user.token)
  if (!store.state.user.token) {
    if (window.localStorage.getItem('myChannels')) {
      // console.log(JSON.parse(window.localStorage.getItem('myChannels') || '[]'))
      return JSON.parse(window.localStorage.getItem('myChannels') || '[]')
    } else {
      const [err, data] = await request({
        url: 'user/channels'
      })
      if (err) throw new Error(err)
      // console.log(data.data.channels)

      window.localStorage.setItem('myChannels', JSON.stringify(data.data.channels))

      return data.data.channels
    }
  } else {
    const [, data] = await request({
      url: 'user/channels'
    })
    return data.data.channels
  }
}

export async function addChannel (channels) {
  if (!store.state.user.token) {
    // 将最新的myChannels 更新到本地存储中去
    return window.localStorage.setItem('myChannels', JSON.stringify(channels))
  } else {
    const list = []
    channels.forEach((item, index) => {
      list.push({
        id: item.id,
        seq: index + 1
      })
    })

    const [, data] = await request({
      url: 'user/channels',
      method: 'PUT',
      data: {
        channels: list
      }
    })
    // 如果服务器更新成功，那么同时更新本地信息
    if (data) {
      window.localStorage.setItem('myChannels', JSON.stringify(channels))
    }
  }
}

/* eslint-disable camelcase */
export async function deleteChannel (channel_id) {
  // 已经登录 进行网络请求
  if (store.state.user.token) {
    await request({
      url: 'user/channels/' + channel_id,
      method: 'delete'
    })
  }

  // 更新本地数据
  let list = JSON.parse(window.localStorage.getItem('myChannels') || '[]')
  list = list.filter((item) => {
    return item.id !== channel_id
  })

  window.localStorage.setItem('myChannels', JSON.stringify(list))
}

export function followingsAuthor (target, status) {
  if (status) {
    // 关注
    return request({
      url: 'user/followings',
      method: 'post',
      data: {
        target
      }
    })
  } else {
    // 取消关注
    return request({
      url: 'user/followings/' + target,
      method: 'delete'
    })
  }
}

// 获取自己的用户信息
export const getUserData = () => {
  return request({
    url: 'user'
  })
}

// 获取个人资料
export const getUserProfile = () => {
  return request({
    url: 'user/profile'
  })
}

// 上传头像
export const uploadAvator = (formData) => {
  return request({
    url: 'user/photo',
    method: 'PATCH',
    data: formData
  })
}

export const saveUserProfile = (user) => {
  return request({
    url: 'user/profile',
    method: 'PATCH',
    data: user
  })
}

/**
 * 刷新token
 * @param {String} refreshToken - 保存的refresh_token
 * @returns
 */
export const refreshTokenAPI = (refreshToken) => {
  return axios({
    url: baseURL + 'authorizations',
    method: 'put',
    headers: {
      Authorization: `Bearer ${refreshToken}`
    }
  })
}
